package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.doris.FieldMonitorChartEntity;
import com.hllxd.hism.star.basic.service.service.FieldMonitorService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/field_monitor")
public class FieldMonitorController2 {
    @Autowired
    private FieldMonitorService fieldMonitorService;

    @Data
    public static class Params {
        /**
         * store,cpu
         */
        public String type = "cpu";
        public String dt;


        public String biz;
        public String actionType;
        public String logsource;
        public String metric;
        /**
         * day, week, month
         */
        public String period;
    }

    @RestGrpcMapping("/loadMetaData.do")
    public NTSTATUS loadMetaData(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            result.setData(fieldMonitorService.loadMetaData(aa.biz, aa.actionType, aa.logsource,aa.metric));
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/loadMetricTrend.do")
    public NTSTATUS loadMetricTrend(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            FieldMonitorChartEntity chart = fieldMonitorService.loadMetricTrend(aa.biz, aa.actionType, aa.logsource,aa.metric);
            result.setData(chart);
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/loadIncMetricTrend.do")
    public NTSTATUS loadIncMetricTrend(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            FieldMonitorChartEntity chart = fieldMonitorService.loadIncMetricTrend(aa.biz, aa.actionType, aa.logsource,aa.metric);
            result.setData(chart);
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/loadRatioMetricTrend.do")
    public NTSTATUS loadRatioMetricTrend(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            FieldMonitorChartEntity chart = fieldMonitorService.loadRatioMetricTrend(aa.biz, aa.actionType, aa.logsource,aa.metric);
            result.setData(chart);
            log.info("success.");
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


}
